﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Snake
{
    public enum direction
    {
        UP = 0x00,
        DOWN = 0x01,
        RIGHT = 0x02,
        LEFT = 0x03,
        NONE = 0x04
    }
    class Game
    {
        public Bitmap gameImage;
        public int[] snakeBodyX=new int[400];
        public int[] snakeBodyY = new int[400];
        int i;
        public int level = 1;
        int score = 0;
        int SnakeHeadX,SnakeHeadY, SnakeLength=1;
        int foodX, foodY;
        int incrementLength;
        Random rC = new Random(DateTime.Now.Millisecond);
        public direction snakeDirection;
        public Game()
        {
            
        }
        public void newSnake()
        {
            this.snakeDirection = direction.NONE;
            SnakeHeadX = rC.Next(20);
            SnakeHeadY = rC.Next(20);
            snakeBodyX[0] = SnakeHeadX;
            snakeBodyY[0] = SnakeHeadY;
            score = 0;
            level = 1;

            SnakeLength = 1;
            
            generateFood();            
        }
        public void stepSnake()
        {

            if (snakeDirection == direction.UP)
            {
                SnakeHeadY--;
            }
            else if (snakeDirection == direction.DOWN)
            {
                SnakeHeadY++;
            }
            else if (snakeDirection == direction.LEFT)
            {
                SnakeHeadX--;
            }
            else if (snakeDirection == direction.RIGHT)
            {
                SnakeHeadX++;
            }
            checkState();
            if (incrementLength == 1)
            {
                incrementLength = 0;
                for (i = SnakeLength; i >0; i--)
                {
                    snakeBodyX[i + 1] = snakeBodyX[i];
                    snakeBodyY[i + 1] = snakeBodyY[i];
                }
                snakeBodyX[0] = SnakeHeadX;
                snakeBodyY[0] = SnakeHeadY;

            }
            else
            {
                for (i = SnakeLength; i >=0 ; i--)
                {
                    snakeBodyX[i + 1] = snakeBodyX[i];
                    snakeBodyY[i + 1] = snakeBodyY[i];
                }
                snakeBodyX[0] = SnakeHeadX;
                snakeBodyY[0] = SnakeHeadY;
            }
        }
        void generateFood()
        {
            foodX = rC.Next(20);
            foodY = rC.Next(20);
            
        }
        public void checkState()
        {
            if (SnakeHeadX == foodX && SnakeHeadY == foodY)
            {
                SnakeLength++;
                incrementLength = 1;
                generateFood();
                score = score + level * 5;
                if (score > (level * 50))
                {
                    level++;
                }
            }
            if (SnakeHeadX == 20 || SnakeHeadY == 20 || SnakeHeadX == -1 || SnakeHeadY == -1)
            {
                newSnake();
            }
            for (i = 0; i < SnakeLength; i++)
            {
                if (SnakeHeadX == snakeBodyX[i] && SnakeHeadY == snakeBodyY[i])
                {
                    newSnake();
                }
            }
        }
        public void refreshBitmap()
        {
            gameImage = new Bitmap(265, 264);
            Graphics g = Graphics.FromImage(gameImage);
            g.FillRectangle(Brushes.LemonChiffon, 0, 0, 265, 265);
            g.FillRectangle(Brushes.White, 0, 0, 240, 240);
            for (i = 0; i < SnakeLength; i++)
            {
                g.FillRectangle(Brushes.Black, snakeBodyX[i] * 12, snakeBodyY[i] * 12, 12, 12);
            }
            g.FillRectangle(Brushes.Green, foodX * 12, foodY * 12, 12, 12);
            g.DrawString("Level:" + level.ToString() + " Score:" + score.ToString(), new Font("Impact", 16), Brushes.RoyalBlue, new PointF(5, 240));        
        }
    }
}
